/**
  ******************************************************************************
  * @file    gt32f030_basetim.c
  * @author  GT Application Team
  * @version V1.0.0
  * @date    03-January-2025      
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 Giantec Semicondutor Inc</center></h2>
  *
  *             http://www.giantec-semi.com/
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/ 
#include "gt32f030_rcc.h"
#include "gt32f030_basetim.h"


/** @addtogroup GT32F030_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BTIM_SR_IT       ((unsigned int)0x00000001)
#define BTIM_CR_IT       ((unsigned int)0x00000020)
#define BTIM_CR_RUN      ((unsigned int)0x00000080)

/** @defgroup BTIM 
  * @brief BTIM driver modules
  * @{
  */ 

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup BTIM_Private_Functions
  * @{
  */ 

/** @defgroup BTIM_Init Initialization functions
 *  @brief   Initialization and functions
 *
@verbatim    
 ===============================================================================
              ##### Initialization functions #####
 ===============================================================================
@endverbatim
  * @{
  */
void BTIM_DeInit(void)
{
	RCC_PeriphResetCmd(RCC_PeriphReset_BASETIM, ENABLE);
	RCC_PeriphResetCmd(RCC_PeriphReset_BASETIM, DISABLE);
}


/**
  * @brief  Initializes the BTIMx peripheral according to the specified
  *         parameters in the BTIM_InitStruct .
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @param  BTIM_InitStruct: pointer to a BTIM_InitTypeDef structure that contains
  *         the configuration information for the specified BTIM peripheral.
  * @retval None
  */
void BTIM_Init(BTIM_TypeDef* BTIMx,BTIM_InitTypeDef* BTIM_InitStruct)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	assert_param(IS_BTIM_GATEPOLE(BTIM_InitStruct->BTIM_GatePole));
	assert_param(IS_BTIM_FUNCTIONMODE(BTIM_InitStruct->BTIM_FunctionMode));
	assert_param(IS_BTIM_LOADMODE(BTIM_InitStruct->BTIM_LoadMode));
	assert_param(IS_BTIM_COUNTSIZE(BTIM_InitStruct->BTIM_CountSize));
	assert_param(IS_BTIM_RUNMODE(BTIM_InitStruct->BTIM_RunMode));
	
	assert_param(IS_FUNCTIONAL_STATE(BTIM_InitStruct->BTIM_GateCmd));
	assert_param(IS_FUNCTIONAL_STATE(BTIM_InitStruct->BTIM_ToggleCmd));
	assert_param(IS_BTIM_CLKPRSC(BTIM_InitStruct->BTIM_ClkPrescale));
	
	BTIMx->CR =  (BTIM_InitStruct->BTIM_GatePole<<11)    | \
	              (BTIM_InitStruct->BTIM_GateCmd<<10)     | \
	              (BTIM_InitStruct->BTIM_ToggleCmd<<9)    | \
	              (BTIM_InitStruct->BTIM_FunctionMode<<8) | \
	              (BTIM_InitStruct->BTIM_LoadMode<<6)     | \
	              (BTIM_InitStruct->BTIM_CountSize<<4)    | \
	              (BTIM_InitStruct->BTIM_RunMode<<3)      | \
	              (BTIM_InitStruct->BTIM_ClkPrescale) ;
								
  BTIMx->LOAD    = BTIM_InitStruct->BTIM_LoadVal;
  BTIMx->BGLOAD  = BTIM_InitStruct->BTIM_BGLoadVal;	
}

/**
  * @brief  Fills each BTIM_InitStruct member with its default value.
  * @param  BTIM_InitStruct: pointer to a BTIM_InitTypeDef structure which will
  *         be initialized.
  * @retval None
  */
void BTIM_StructInit(BTIM_InitTypeDef* BTIM_InitStruct)
{
	BTIM_InitStruct->BTIM_GatePole     = BTIM_GatePole_Low;
	BTIM_InitStruct->BTIM_FunctionMode = BTIM_FunctionMode_Timer;
	BTIM_InitStruct->BTIM_LoadMode     = BTIM_LoadMode_FreeRun;
	BTIM_InitStruct->BTIM_CountSize    = BTIM_CountSize_16Bit;
	BTIM_InitStruct->BTIM_RunMode      = BTIM_RunMode_Repeate;
	BTIM_InitStruct->BTIM_ClkPrescale  = BTIM_ClkPrescale_div1;

	BTIM_InitStruct->BTIM_GateCmd     = DISABLE;
	BTIM_InitStruct->BTIM_ToggleCmd   = DISABLE;	
  BTIM_InitStruct->BTIM_LoadVal     = 0x00;
  BTIM_InitStruct->BTIM_BGLoadVal   = 0x00;
}

/**
  * @}  
  */





/** @defgroup BTIM_OpCfg Operation and Configuration functions
 *  @brief   Operation and Configuration functions
 *
@verbatim   
 ===============================================================================
           ##### Operation and Configuration functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Enables or disables the specified BTIM peripheral.
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @param  NewState: new state of the BTIM.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void BTIM_Cmd(BTIM_TypeDef* BTIMx,FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    BTIMx->CR |= BTIM_CR_RUN;
  }
  else
  {
    BTIMx->CR &= ~BTIM_CR_RUN;
  }	
}

/**
  * @brief  Set the load value of the specified BTIM
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
	* @param  LoadVal: the BTIM load value.
  * @retval None
  */
void BTIM_SetLoadVal(BTIM_TypeDef* BTIMx,unsigned int LoadVal)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	
  BTIMx->LOAD = LoadVal;
}

/**
  * @brief  Set the background load value of the specified BTIM
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @param  BGLoadVal: the BTIM background value.
  * @retval None
  */
void BTIM_SetBGLoadVal(BTIM_TypeDef* BTIMx,unsigned int BGLoadVal)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
  
	BTIMx->BGLOAD = BGLoadVal;
}

/**
  * @brief  Get the counter value of the specified BTIM
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @retval the BTIM counter value
  */
unsigned int BTIM_GetCountVal(BTIM_TypeDef* BTIMx)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	
	return BTIMx->CNT;
}

/**
  * @}  
  */



/** @defgroup  BTIM_IntrFlag Interrupts and flags management functions
 *  @brief    Interrupts and flags management functions
 *
@verbatim   
 ===============================================================================
           #####  Interrupts and flags management functions #####
 ===============================================================================  
@endverbatim
  * @{
  */
/**
  * @brief  Enables or disables the BTIM interrupts.
  * @param  NewState: new state of the BTIM.
  *         This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void BTIM_ITConfig(BTIM_TypeDef* BTIMx,FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  if (NewState != DISABLE)
  {
    BTIMx->CR |= BTIM_CR_IT;
  }
  else
  {
    BTIMx->CR &= ~BTIM_CR_IT;
  }		
}

/**
  * @brief  Checks whether the BTIM interrupt flag is set or not.
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @retval The new state of BTIM interrupt flag (SET or RESET).
  */
ITStatus BTIM_GetIT(BTIM_TypeDef* BTIMx)
{
  ITStatus bitstatus = RESET;
	/* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	
  if ((BTIMx->INTMASKSR & BTIM_SR_IT) != (unsigned int)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return  bitstatus;		
}

/**
  * @brief  Checks whether the BTIM RAW interrupt flag is set or not.
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @retval The new state of BTIM RAW interrupt flag (SET or RESET).
  */
ITStatus BTIM_GetRawIT(BTIM_TypeDef* BTIMx)
{
  FlagStatus bitstatus = RESET;
	/* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	
  if ((BTIMx->INTRAWSR & BTIM_SR_IT) != (unsigned int)RESET)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return  bitstatus;	
}

/**
  * @brief  Clears the BTIM interrupt flags.
  * @param  BTIMx: This parameter can be: TIM10 or TIM11
  * @retval None.
  */
void BTIM_ClearIT(BTIM_TypeDef* BTIMx)
{
	/* Check the parameters */
  assert_param(IS_BTIM_ALL_PERIPH(BTIMx));
	
	BTIMx->INTCLR = 0x01;
}

/**
  * @}  
  */ 

/**
  * @}  
  */

/**
  * @}  
  */ 

/**
  * @}
  */ 
/************************ (C) COPYRIGHT Giantec Semicondutor Inc *****END OF FILE****/
